package hospital.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hospital.cmnclient.DictFeignClient;
import hospital.enums.DictEnum;
import hospital.model.user.Patient;
import hospital.model.user.UserInfo;
import hospital.user.mapper.PatientMapper;
import hospital.user.mapper.UserInfoMapper;
import hospital.user.service.PatientService;
import hospital.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author
 * @description:
 */
@Service
public class PatientServiceImpl extends
        ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private DictFeignClient dictFeignClient;
    /**
     * @Author yang
     * @Description //TODO 通过userid获取就诊人列表
     * @Param [userId]
     * @return
     **/
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        //根据user_id,查询就诊人的信息
        QueryWrapper<Patient> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patients = baseMapper.selectList(wrapper);

        ////通过远程调用，得到编码对应具体内容，查询数据字典表内容
        patients.stream().forEach(item ->{
            //包装Patient的其他参数
            this.packetPatient(item);

        });
        return patients;
    }

    /**
     * @Author yang
     * @Description //TODO 通过就诊人的id获取，就诊人的信息
     * @Param [id]
     * @return
     **/
    @Override
    public Patient getPatientInfo(Long id) {
        Patient patient = baseMapper.selectById(id);
        return this.packetPatient(patient);
    }

    /**
     * @Author yang
     * @Description //TODO 根据就诊人id获取就诊人信息
     * @Param [id]
     * @return
     **/
    @Override
    public Patient getPatientOrder(Long id) {
        return this.getPatientInfo(id);
    }

    /**
     * @Author yang
     * @Description //TODO //通过远程调用，得到编码对应具体内容，查询数据字典表内容
     * @Param [item]
     * @return
     **/
    private Patient packetPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeString = dictFeignClient.getDictName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());

        //省code
        String provinceCode = dictFeignClient.getDictName(patient.getProvinceCode());
        //市code
        String cityCode = dictFeignClient.getDictName(patient.getCityCode());
        // 区code
        String districtCode = dictFeignClient.getDictName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceCode);
        patient.getParam().put("cityString", cityCode);
        patient.getParam().put("districtString", districtCode);
        patient.getParam().put("fullAddress", provinceCode + cityCode + districtCode + patient.getAddress());
        return patient;
    }
}
